/**
 * Created by George on 2018/3/18.
 */
define(['angular', 'util', 'jquery', 'ueditor-config', 'ueditor'], function (angular, util, jquery, ueditorConfig, ueditor) {
    "use strict";
    // 将本控制器函数作为结果返回给router.js
    var createNoteCtrl = function ($scope, $http) {
        $scope.welcome_info = "笔记管理";
        $scope.note = {
            name: "",
            date: util.getNowFormatDate(),
            content: ""
        };
        UE.delEditor('container');
        $scope.container = UE.getEditor('container', {
            //这里可以选择自己需要的工具按钮名称,此处仅选择如下七个
            toolbars: [['fullscreen', 'source', 'undo', 'redo', 'bold', 'italic', 'underline']],
            //focus时自动清空初始化时的内容
            autoClearinitialContent: true,
            //关闭字数统计
            wordCount: false,
            //关闭elementPath
            elementPathEnabled: false,
            //默认的编辑区域高度
            initialFrameHeight: 200,
            initialFrameWidth: getTextWidth(),
            scaleEnabled: false,
            serverUrl: ""
            //更多其他参数，请参考umeditor.config.js中的配置项
        });

        function getTextWidth() {
            return $("#noteContent").width() * 0.8;
        }

        $scope.getContent = function () {
            var arr = [];
            arr.push("使用editor.getContent()方法可以获得编辑器的内容");
            arr.push("内容为：");
            arr.push(container.getContent());
            alert(arr.join("\n"));
        };

        $scope.addNoteClick = function () {
            $scope.note.content = $scope.container.getContent();
            console.log($scope.note);
            var data = {
                "name": $scope.note.name,
                "content": $scope.note.content,
                "date": $scope.note.date
            };
            $http.post('/basketballfire/rest/note', data).then(function (req) {
                console.log("success!");
                alert("创建成功！");
                window.location.href = "/basketballfire/index.html#/note";
            }, function (req) {
                console.log("failed!");
                alert("创建失败！");
            });
        };

        $scope.CancelClick = function () {
            console.log("click cancel");
            window.location.href = "/basketballfire/index.html#/note";
        };

        function init() {
            initOrUpdateData();
        }

        function initOrUpdateData() {
            //TODO
        }


        //app start
        init();
    };

    return createNoteCtrl;
});